import axios from "axios";
import router from "@/router";

// 创建 axios 实例
const service = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 50000,
  headers: { "Content-Type": "application/json;charset=utf-8" },
});

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    //   const accessToken = getToken();
    //   // 如果 Authorization 设置为 no-auth，则不携带 Token，用于登录、刷新 Token 等接口
    //   if (config.headers.Authorization !== "no-auth" && accessToken) {
    //     config.headers.Authorization = accessToken;
    //   } else {
    //     delete config.headers.Authorization;
    //   }
    return config;
  },
  (error) => Promise.reject(error)
);

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    const { status, data } = response;
    if (status == 200) {
      return data;
    }
    return Promise.reject("cuowu");
  },
  async (error) => {
    // 非 2xx 状态码处理 401、403、500 等
    const { config, response } = error;
    if (response) {
      //     const { code, msg } = response.data;
      //     if (code === ResultEnum.ACCESS_TOKEN_INVALID) {
      //       // Token 过期，刷新 Token
      //       return handleTokenRefresh(config);
      //     } else if (code === ResultEnum.REFRESH_TOKEN_INVALID) {
      //       return Promise.reject(new Error(msg || "Error"));
      //     } else {
      //       ElMessage.error(msg || "系统出错");
      //     }
    }
    return Promise.reject(error.message);
  }
);

export default service;
